package com.jingdianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.context.LoginContextHolder;
import com.jingdianjichi.subject.application.convert.SubjectCategoryDTOConverter;
import com.jingdianjichi.subject.application.convert.SubjectLabelDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectCategoryDTO;
import com.jingdianjichi.subject.application.dto.SubjectLabelDTO;
import com.jingdianjichi.subject.application.utils.LoginUtil;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.common.enums.IsDeletdFlageEnum;
import com.jingdianjichi.subject.domain.convert.SubjectLabelConverter;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * SubjectLabelController 类
 *
 * @author 熊苏鹏1
 * @date 2025/2/14 20:37
 * @description 标签Controller
 */
@RestController
@RequestMapping("/subject/label")
@Slf4j
public class SubjectLabelController {
    @Resource
    SubjectLabelDomainService subjectLabelDomainService;
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {

            if (log.isInfoEnabled())
                log.info("SubjectLabelController add dto:{}", JSON.toJSONString(subjectLabelDTO));
            Preconditions.checkNotNull(subjectLabelDTO.getLabelName(), "标签名称不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDTOToBo(subjectLabelDTO);
            boolean result = subjectLabelDomainService.add(subjectLabelBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectCategoryController add error:{}", e.getMessage(), e);
            return Result.fail(false);

        }
    }
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {

            if (log.isInfoEnabled())
                log.info("SubjectLabelController update dto:{}", JSON.toJSONString(subjectLabelDTO));
            Preconditions.checkNotNull(subjectLabelDTO.getId(), "更新标签Id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDTOToBo(subjectLabelDTO);
            boolean result = subjectLabelDomainService.update(subjectLabelBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectCategoryController update error:{}", e.getMessage(), e);
            return Result.fail(false);

        }
    }
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {

            if (log.isInfoEnabled())
                log.info("SubjectLabelController delete dto:{}", JSON.toJSONString(subjectLabelDTO));
            Preconditions.checkNotNull(subjectLabelDTO.getId(), "删除标签Id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDTOToBo(subjectLabelDTO);
            boolean result = subjectLabelDomainService.delete(subjectLabelBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectCategoryController delete error:{}", e.getMessage(), e);
            return Result.fail(false);

        }
    }

    /**
     * 根据分类Id查询标签
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/queryLabelByCategoryId")
    public Result queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            String loginId = LoginUtil.getLoginId();
            if (log.isInfoEnabled())
                log.info("SubjectLabelController queryLabelByCategoryId dto:{}", JSON.toJSONString(subjectLabelDTO));
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(), "查询分类下标签的Id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDTOToBo(subjectLabelDTO);
            List<SubjectLabelBO> resultList = subjectLabelDomainService.queryLabelByCategoryId(subjectLabelBO);
            List<SubjectLabelDTO>  subjectLabelDTO1 = SubjectLabelDTOConverter.INSTANCE.convertBoToDtoList(resultList);
            return Result.ok(subjectLabelDTO1);
        } catch (Exception e) {
            log.error("SubjectCategoryController queryLabelByCategoryId error:{}", e.getMessage(), e);
            return Result.fail(false);

        }
    }
}
